﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Ice.Toolkit.Data.ILInvoker;

namespace Ice.Toolkit.Data.Entity
{
    public class EntityBase<T> where T : EntityBase<T>, new()
    {
        public static T Create()
        {
            var entity = new T();
           
            foreach (var property in typeof(T).GetProperties())
            {
                foreach (var customAttrib in property.GetCustomAttributes(true))
                {
                    if (customAttrib is EntityColumn)
                    {
                        var clnAtrrib = (customAttrib as EntityColumn);

                        if (clnAtrrib.IsIdentity && clnAtrrib.DbType == System.Data.DbType.String)
                        {
                            //PropertyAccessor<T>.Set(entity, property.Name, Guid.NewGuid());
                            property.SetValue(entity, Guid.NewGuid().ToString(), null);
                        }
                    }
                }
            }

            return entity;
        }
    }
}
